from django.shortcuts import render
from PIL import Image
# Create your views here.

from django.shortcuts import render
import json
import os
from django.http import HttpResponse, JsonResponse


# Create your views here.


def login(request):
	if request.method != "POST":
		return HttpResponse('请使用post方法提交')

	data = json.loads(request.body)
	print(data, type(data))
	username = data.get('username')
	password = data.get('password')
	print(username, password)
	if username == "czq" and password == '123':
		data = {'code': 1, "msg": "登录成功"}
		return JsonResponse(data)
	else:
		data = {'code': 0, "msg": "账号密码不正确"}

		return JsonResponse(data)


def logout(request):
    """
    用户注销，删除session信息，重定向到登录页面
    """
    if request.session.get('name') or request.session.get('username'):
        request.session.clear()
        request.session.flush()
        return JsonResponse({'code': 1, "msg": "退出成功"})
    else:
        return JsonResponse({'code': 0, "msg": "请先登录"})

def receive_text(request):
	if request.method != "POST":
		return HttpResponse('请使用post方法提交')

	file = request.FILES.get('file')
	from django.core.files.storage import FileSystemStorage
	print(file.name)
	fs = FileSystemStorage()
	fs.save(file.name, file)

	print(type(file))
	print(request.content_type)
	return JsonResponse({'code': 0, "msg": "账号密码不正确"})